﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KenLib.BusinessManager;
using DataInfo;

public partial class frmD_CompLoginManager : System.Web.UI.Page
{
    本社情報Manager _本社情報Manager = new 本社情報Manager();
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Page load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        ((MasterPage)this.Master).changeTitle("企業ログイン管理");
        if (Session["役"] == null || Session["役"].ToString() == "")
        {
            Response.Redirect("frmD_Login.aspx");
        }
        ddl_企業分類.Focus();
        if (!IsPostBack)
        {
            ShowGrid();
            // ddl for 1,3,4
            Session["企業分類"] = ddl_企業分類.SelectedValue;
            // Check pass go to page frmD_CompLoginManager true or false
            Session["temp"] = 1;
        }
    }


    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Search data
    /// </summary>
    /// <param name="企業分類コード"></param>
    /// <param name="企業コード"></param>
    /// <param name="企業名"></param>
    /// <returns></returns>
    public DataTable frmD_CompLoginManager_Search(int 企業分類コード, string 企業コード, string 企業名)
    {
        DataTable dt = new DataTable();
        dt = _本社情報Manager.frmD_CompLoginManager_Search(企業分類コード, 企業コード, 企業名);
        return dt;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Show data in gridview
    /// </summary>
    public void ShowGrid()
    {
        int 企業分類コード = 0;
        string 企業コード = "";
        string 企業名 = "";
        //if (ddl_企業分類.SelectedValue != "-1")
        //{
        //    企業分類コード = Convert.ToInt32(ddl_企業分類.SelectedValue);
        //}
        if (Session["frm_MRecCompManager_flag"]!=null && Session["frm_MRecCompManager_flag"].ToString() == "1")
        {
            企業分類コード = 1;
            //ddl_企業分類.SelectedItem.Text = "実習実施機関";
            lbl_企業分類.Text = "実習実施機関";
            lbl_企業分類.Visible = true;
            ddl_企業分類.Visible = false;
        }

        else if (Session["frm_MJointCompManager_flag"] != null && Session["frm_MJointCompManager_flag"].ToString() == "1")
        {
            企業分類コード = 3;
            //ddl_企業分類.SelectedItem.Text = "送出し機関";
            lbl_企業分類.Text = "送出し機関";
            lbl_企業分類.Visible = true;
            ddl_企業分類.Visible = false;
        }

        else if (Session["frm_MOrgManager_flag"] != null && Session["frm_MOrgManager_flag"].ToString() == "1")
        {
            企業分類コード = 4;
            //ddl_企業分類.SelectedItem.Text = "監理団体";
            lbl_企業分類.Text = "監理団体";
            lbl_企業分類.Visible = true;
            ddl_企業分類.Visible = false;
        }
        else
        {
            ddl_企業分類.Visible = true;
            lbl_企業分類.Visible = false;
            if (ddl_企業分類.SelectedValue != "-1")
            {
                企業分類コード = Convert.ToInt32(ddl_企業分類.SelectedValue);
            }
        }
        
        if (txt_企業コード.Text == "")
        {
            企業コード = "";
        }
        else
        {
            企業コード = ConvertHTMLtoText(txt_企業コード.Text);
        }

        if (txt_企業名.Text == "")
        {
            企業名 = "";
        }
        else
        {
            企業名 = ConvertHTMLtoText(txt_企業名.Text);
        }

        DataTable dt = frmD_CompLoginManager_Search(企業分類コード, 企業コード, 企業名);
        //DataTable dt = new DataTable();
        if (dt.Rows.Count == 0)
        {
            //AppCode_Common.ShowEmtyGrid<本社情報>(grv, 0, "データがありません。", "");

            string strHeader = "企業コード,企業名,企業分類コード,郵便番号,ＴＥＬ,イベント,企業ID";
            ShowGridViewNotClassEmpty(grv, strHeader, "データがありません。");
        }
        else
        {
            grv.DataSource = dt;
            grv.DataBind();
        }
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Paging
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv.PageIndex = e.NewPageIndex;
        ShowGrid();
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Search data
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_検索_Click(object sender, EventArgs e)
    {
        ShowGrid();
        //ddl cho lua chon 1,3,4
        if (Session["frm_MRecCompManager_flag"] != null && Session["frm_MRecCompManager_flag"].ToString() == "1")
        {
            Session["企業分類"] = 1;
        }
        else if (Session["frm_MJointCompManager_flag"] != null && Session["frm_MJointCompManager_flag"].ToString() == "1")
        {
            Session["企業分類"] = 3;
        }
        else if (Session["frm_MOrgManager_flag"] != null && Session["frm_MOrgManager_flag"].ToString() == "1")
        {
            Session["企業分類"] = 4;
        }
        else
        {
            Session["企業分類"] = ddl_企業分類.SelectedValue;
        }
        
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Button1
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["役"].ToString() != null && Session["役"].ToString() == "1")
        {
            Session.Remove("frm_MOrgManager_flag");
            Session.Remove("frm_MJointCompManager_flag");
            Session.Remove("frm_MRecCompManager_flag");
            lbl_企業分類.Visible = false;
            ddl_企業分類.Visible = true;
            Response.Redirect("frm_MainMenu.aspx");
        }
        //else if (Session["frm_MRecCompManager_flag"] != null && Session["frm_MRecCompManager_flag"].ToString() == "1")
        //{
        //    //Session["企業分類"] = 1;
        //    Session.Remove("frm_MRecCompManager_flag");
        //    Response.Redirect("frm_MRecCompManager.aspx");
        //}
        //else if (Session["frm_MJointCompManager_flag"] != null && Session["frm_MJointCompManager_flag"].ToString() == "1")
        //{
        //    Session.Remove("frm_MJointCompManager_flag");
        //    Response.Redirect("frm_MJointCompManager.aspx");
        //}
        //else if (Session["frm_MOrgManager_flag"] != null && Session["frm_MOrgManager_flag"].ToString() == "1")
        //{
        //    Session.Remove("frm_MOrgManager_flag");
        //    Response.Redirect("frm_MOrgManager.aspx");
        //}
        else
        {
            Response.Redirect("frmD_Login.aspx");
        }
        //Session.Remove("frm_MRecCompManager_flag");
        //Response.Redirect("frm_MainMenu.aspx");
    }
    
    /// <summary>
    /// Coder: Thanh Dong
    /// Event: Select index change dropdownlist 企業分類
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddl_企業分類_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ddl cho lua chon 1,3,4
        Session["企業分類"] = ddl_企業分類.SelectedValue;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert character html to text
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Show gridview empty
    /// </summary>
    /// <param name="grv"></param>
    /// <param name="strTableHeader"></param>
    /// <param name="strEmpty"></param>
    public void ShowGridViewNotClassEmpty(GridView grv, string strTableHeader, string strEmpty)
    {
        DataTable dt = new DataTable();
        string[] arrColumnNames = strTableHeader.Split(',');
        foreach (string str in arrColumnNames)
        {
            dt.Columns.Add(str);
        }
        grv.ShowFooter = false;
        DataRow r = dt.NewRow();
        dt.Rows.InsertAt(r, 0);
        grv.DataSource = dt;
        grv.DataBind();
        grv.Rows[0].Cells.Clear();
        TableCell cell = new TableCell();
        cell.ColumnSpan = grv.Columns.Count + 1;
        cell.Text = strEmpty;
        grv.Rows[0].Cells.Add(cell);
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Convert number to string
    /// </summary>
    /// <param name="企業分類コード"></param>
    /// <returns></returns>
    public string Convert企業分類コード(string 企業分類コード)
    {
        string temp = "";
        switch(企業分類コード)
        {
            case "1":
                temp = "実習実施機関";
                break;
            case "3":
                temp = "送出し機関";
                break;
            case "4":
                temp = "監理団体";
                break;
            default: 
                temp = "";
                break;
                
        }
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Cut string when string too long
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ShowTextLength(string temp)
    {   
        if (checkContainsUnicodeCharacter(temp))
        {
            //true tieng nhat
            if (temp.Length > 10)
            {
                temp = temp.Substring(0, 10) + "...";
            }
        }
        else
        { 
            //tieng viet
            if (temp.Length > 20)
            {
                temp = temp.Substring(0, 20) + "...";
            }
        }
        
        return temp;
    }

    /// <summary>
    /// Coder: Thanh Dong
    /// Function: Check unicode string
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public bool checkContainsUnicodeCharacter(string input)
    {
        const int MaxAnsiCode = 255;
        char[] a = input.ToCharArray();
        for (int i = 0; i < a.Length; i++)
        {
            if (a[i] > MaxAnsiCode)
            {
                return true;
            }
        }
        return false;
    }

    
     
   

}
